home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_ossaudiodev.py < prev    next >
Text File  |  2005-11-19  |  4KB  |  125 lines

  1. from test import test_support
  2. test_support.requires('audio')
  3.  
  4. from test.test_support import verbose, findfile, TestFailed, TestSkipped
  5.  
  6. import errno
  7. import fcntl
  8. import ossaudiodev
  9. import os
  10. import sys
  11. import select
  12. import sunaudio
  13. import time
  14. import audioop
  15.  
  16. # Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
  17. # fairly recent addition to OSS.
  18. try:
  19.     from ossaudiodev import AFMT_S16_NE
  20. except ImportError:
  21.     if sys.byteorder == "little":
  22.         AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
  23.     else:
  24.         AFMT_S16_NE = ossaudiodev.AFMT_S16_BE
  25.  
  26.  
  27. SND_FORMAT_MULAW_8 = 1
  28.  
  29. def read_sound_file(path):
  30.     fp = open(path, 'rb')
  31.     size, enc, rate, nchannels, extra = sunaudio.gethdr(fp)
  32.     data = fp.read()
  33.     fp.close()
  34.  
  35.     if enc != SND_FORMAT_MULAW_8:
  36.         print "Expect .au file with 8-bit mu-law samples"
  37.         return
  38.  
  39.     # Convert the data to 16-bit signed.
  40.     data = audioop.ulaw2lin(data, 2)
  41.     return (data, rate, 16, nchannels)
  42.  
  43.  
  44. def play_sound_file(data, rate, ssize, nchannels):
  45.     try:
  46.         dsp = ossaudiodev.open('w')
  47.     except IOError, msg:
  48.         if msg[0] in (errno.EACCES, errno.ENODEV, errno.EBUSY):
  49.             raise TestSkipped, msg
  50.         raise TestFailed, msg
  51.  
  52.     # at least check that these methods can be invoked
  53.     dsp.bufsize()
  54.     dsp.obufcount()
  55.     dsp.obuffree()
  56.     dsp.getptr()
  57.     dsp.fileno()
  58.  
  59.     # set parameters based on .au file headers
  60.     dsp.setparameters(AFMT_S16_NE, nchannels, rate)
  61.     t1 = time.time()
  62.     print "playing test sound file..."
  63.     dsp.write(data)
  64.     dsp.close()
  65.     t2 = time.time()
  66.     print "elapsed time: %.1f sec" % (t2-t1)
  67.  
  68. def test_setparameters():
  69.     dsp = ossaudiodev.open("w")
  70.  
  71.     # Two configurations for testing:
  72.     #   config1 (8-bit, mono, 8 kHz) should work on even the most
  73.     #      ancient and crufty sound card, but maybe not on special-
  74.     #      purpose high-end hardware
  75.     #   config2 (16-bit, stereo, 44.1kHz) should work on all but the
  76.     #      most ancient and crufty hardware
  77.     config1 = (ossaudiodev.AFMT_U8, 1, 8000)
  78.     config2 = (AFMT_S16_NE, 2, 44100)
  79.  
  80.     for config in [config1, config2]:
  81.         (fmt, channels, rate) = config
  82.         if (dsp.setfmt(fmt) == fmt and
  83.             dsp.channels(channels) == channels and
  84.             dsp.speed(rate) == rate):
  85.             break
  86.     else:
  87.         raise RuntimeError("unable to set audio sampling parameters: "
  88.                            "you must have really weird audio hardware")
  89.  
  90.     # setparameters() should be able to set this configuration in
  91.     # either strict or non-strict mode.
  92.     result = dsp.setparameters(fmt, channels, rate, False)
  93.     assert result == (fmt, channels, rate), \
  94.            "setparameters%r: returned %r" % (config + result)
  95.     result = dsp.setparameters(fmt, channels, rate, True)
  96.     assert result == (fmt, channels, rate), \
  97.            "setparameters%r: returned %r" % (config + result)
  98.  
  99.     # Now try some configurations that are presumably bogus: eg. 300
  100.     # channels currently exceeds even Hollywood's ambitions, and
  101.     # negative sampling rate is utter nonsense.  setparameters() should
  102.     # accept these in non-strict mode, returning something other than
  103.     # was requested, but should barf in strict mode.
  104.     for config in [(fmt, 300, rate),       # ridiculous nchannels
  105.                    (fmt, -5, rate),        # impossible nchannels
  106.                    (fmt, channels, -50),   # impossible rate
  107.                   ]:
  108.         (fmt, channels, rate) = config
  109.         result = dsp.setparameters(fmt, channels, rate, False)
  110.         assert result != config, \
  111.                "setparameters: unexpectedly got requested configuration"
  112.  
  113.         try:
  114.             result = dsp.setparameters(fmt, channels, rate, True)
  115.             raise AssertionError("setparameters: expected OSSAudioError")
  116.         except ossaudiodev.OSSAudioError, err:
  117.             print "setparameters: got OSSAudioError as expected"
  118.  
  119. def test():
  120.     (data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
  121.     play_sound_file(data, rate, ssize, nchannels)
  122.     test_setparameters()
  123.  
  124. test()
  125.